package com.myWeb.controller;

import com.myWeb.dao.UserDao;
import com.myWeb.entity.Users;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class UserAddServlet extends HttpServlet {


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String userName,password,sex,email;
        UserDao dao = new UserDao();
        int result = 0;

        request.setCharacterEncoding("utf-8");
        //1.调用请求对象，读取请求头参数信息，得到用户信息
        userName = request.getParameter("userName");
        password = request.getParameter("password");
        sex = request.getParameter("sex");
        email = request.getParameter("email");

        //2.将用户信息传到用户实体类中，再通过DAO类将用户对象传到数据库中
        Users user = new Users(null,userName,password,sex,email);
        Date startDate = new Date();
        result = dao.add(user,request);
        Date endDate = new Date();
        System.out.println("注册消耗时间" + (endDate.getTime()-startDate.getTime()) + "毫秒");
        //3.调用响应对象 将处理结果写进响应体
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        if(result == 1){
            request.getSession();
            out.println("<font style='color:green;size:40'>用户注册成功</font>");
            out.print("<font style='color:green;size:40'><a href=" + "/myWeb/login.html" + ">返回登录页面</a></font>");
        }else{
            out.print("<font style='color:red;size:40'>用户注册失败</font>");
            out.print("<font style='color:green;size:40'><a href=" + "/myWeb/user_Add.html" + ">返回注册页面</a></font>");
        }



    }





}
